package smack.client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;

import org.jivesoftware.smack.packet.Message;

public class JTalkTabbedPane extends JTabbedPane {
	/**
	 * 
	 */
	private static final long serialVersionUID = 2555921984551688288L;
	
	public final static int MAX_TAB = 9 ;

	public LogPane addChat(String uid,String name){
		if(this.getTabCount() == MAX_TAB){
			return null ;
		}
		LogPane logPanel = new LogPane(uid,name);
        
        this.addTab(name, logPanel); //
        
        resetMnemonic();
        
        return logPanel ; 
	}
	
	public void resetMnemonic(){
		int count = this.getTabCount();
		for(int i=0;i<count;i++){
			setMnemonicAt(i,KeyEvent.VK_0+i+1);
		}
	}
	
	public String getCurrentContent(){
		return ((LogPane)this.getSelectedComponent()).getText();
	}
	
	public void setText(String msg){
		if(this.getSelectedComponent()!=null){
			((LogPane)this.getSelectedComponent()).setText(msg);
		}
	}
	
	public LogPane find(String name){
		System.out.println("find tab:"+name);
		for(int i=0;i<this.getTabCount();i++){
			LogPane p = ((LogPane)this.getComponent(i));
			if(name.equalsIgnoreCase(p.getText())){
				return p ;
			}
		}
		return null ;
	}
	
	class LogPane extends JPanel{
		/**
		 * 
		 */
		private static final long serialVersionUID = 5989052311206687836L;
		
		private JTextArea logTextArea ;
		private JLabel logLabel ;
		
		private String uid ;
		private String name ;
		public LogPane(String uid,String name){
			this.uid = uid;
			this.name = name ;
	        setLayout(new BorderLayout());
	        setBorder(BorderFactory.createLoweredBevelBorder());
	        
	        FlowLayout flowLayout = new FlowLayout();
	        flowLayout.setAlignment(FlowLayout.LEFT);
	        
	        logLabel = new JLabel(name);
	        logLabel.setLayout(flowLayout);
	        logLabel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
	        logLabel.setAlignmentX(Component.LEFT_ALIGNMENT);
	        add(logLabel, BorderLayout.NORTH);

	        logTextArea = new JTextArea();
	        logTextArea.setEditable(false);
	        logTextArea.setBackground(new Color(211, 211, 211));
	        logTextArea.setLineWrap(true);
	        logTextArea.setMaximumSize(new Dimension(0, 100));
	        logTextArea.setMinimumSize(new Dimension(0, 100));
	        JScrollPane incomingTextAreaScroll = new JScrollPane(logTextArea, 
	                                                             JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
	                                                             JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
	        add(incomingTextAreaScroll, BorderLayout.CENTER);
		}
		
		public void appendMessage(String from ,String msg) {
			logTextArea.append(System.currentTimeMillis() + "\t"+from+" : "+ msg + " \n ");
		}

		public void setText(String msg) {
			logLabel.setText(msg);
		}

		public String getText(){
			return logLabel.getText();
		}

		public String getUid() {
			return uid;
		}

		public void setUid(String uid) {
			this.uid = uid;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
			logLabel.setText(name);
		}
		
	}
}

